package cn.com.inossem.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.support.util.Base64;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import cn.com.inossem.activity.FriendChatActivity;
import cn.com.inossem.activity.R;
import cn.com.inossem.activity.XiTongMessageActivity;
import cn.com.inossem.model.LiaotianInfoBean;
import cn.com.inossem.utils.Utils;

public class MessageListAdapter extends BaseAdapter {
	private Context mContext;
	private LayoutInflater mInflater;
	private List<LiaotianInfoBean> messageInfo;

	public MessageListAdapter(Context mContext,
			List<LiaotianInfoBean> messageInfo) {
		super();
		this.mContext = mContext;
		this.messageInfo = messageInfo;
		mInflater = LayoutInflater.from(mContext);
	}

	@Override
	public int getCount() {
		// return messageInfo.size();
		System.out.println(" messageInfo.size() + 1:" + messageInfo.size() + 1);
		return messageInfo.size() + 1;
	}

	@Override
	public LiaotianInfoBean getItem(int arg0) {
		return messageInfo.get(arg0);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			holder = new ViewHolder();
			convertView = mInflater.inflate(R.layout.message_item_layout, null);
			holder.rlayout = (RelativeLayout) convertView
					.findViewById(R.id.rlayout);
			holder.title = (TextView) convertView.findViewById(R.id.title);
			holder.content = (TextView) convertView.findViewById(R.id.content);
			holder.time = (TextView) convertView.findViewById(R.id.time);
			holder.img = (ImageView) convertView.findViewById(R.id.img);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		if (position == 0) {
			holder.content.setVisibility(View.GONE);
			holder.time.setVisibility(View.GONE);
			holder.rlayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(mContext,
							XiTongMessageActivity.class);
					mContext.startActivity(intent);
				}
			});
		} else {
			holder.content.setVisibility(View.VISIBLE);
			holder.time.setVisibility(View.VISIBLE);

			if (!getItem(position - 1).getImageData().equals("")) {
				holder.img.setBackgroundDrawable(new BitmapDrawable(Utils
						.toRoundCorner(stringtoBitmap(getItem(position - 1)
								.getImageData()), 300)));
			}

			holder.title.setText(getItem(position - 1).getTruename());
			holder.content.setText(getItem(position - 1).getLastMsg());
			holder.time.setText(getItem(position - 1).getLastDate().substring(
					0, 10));

			holder.rlayout.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					Intent it = new Intent(mContext, FriendChatActivity.class);
					it.putExtra("toUser", getItem(position - 1).getOtherId());
					it.putExtra("toUserName", getItem(position - 1)
							.getTruename());
					it.putExtra("toUserHeader", getItem(position - 1)
							.getImageData());
					mContext.startActivity(it);
				}
			});
		}

		return convertView;
	}

	public Bitmap stringtoBitmap(String string) {
		// 将字符串转换成Bitmap类型
		Bitmap bitmap = null;
		try {
			byte[] bitmapArray;
			bitmapArray = Base64.decode(string, Base64.DEFAULT);
			bitmap = BitmapFactory.decodeByteArray(bitmapArray, 0,
					bitmapArray.length);
		} catch (Exception e) {
			e.printStackTrace();
		}

		return bitmap;
	}

	public final class ViewHolder {
		RelativeLayout rlayout;
		public ImageView img;
		public TextView title;
		public TextView content;
		public TextView time;
	}
}
